// pages/shop-buy/shop-buy.js
import { Shop } from '../../models/shop'
import { Publish } from '../../models/other'

const shopModel = new Shop()

const publishModel = new Publish()

Page({
  data: {
    showLoading: false,
    showDialog: false,
    showSkeleton: true,
    show: false,
    data: [],
    page: 1,
    cityList: [
      '河南',
      '广东',
      '江苏',
      '河北',
      '浙江',
      '上海',
      '山东',
      '福建',
      '江西',
      '陕西',
      '湖南',
      '四川',
      '湖北',
      '贵州',
      '安徽',
      '北京',
      '山西',
      '广西',
      '甘肃',
      '辽宁',
      '吉林',
      '重庆',
      '天津',
      '黑龙江',
      '云南',
      '内蒙古',
      '海南',
      '宁夏',
      '西藏',
      '青海',
      '新疆',
      '香港',
      '澳门',
      '台湾'
    ],
    areaList: [],
    wayList: [],
    searchValue: '',
    checkedCity: '',
    checkedArea: '',
    checkedWay: '',
    shareTitle: '收购足浴店',
    shareImg: '',
    sharePath: '/pages/shop-buy/shop-buy'
  },
  onLoad(options) {
    this.init()
  },

  onPullDownRefresh() {
    this.setData({
      page: 1
    })
    this.search()
  },

  onReachBottom() {
    this.setData({
      page: this.data.page + 1
    })
    this.search('more')
  },

  toNewPage(event) {
    let path = event.currentTarget.dataset.path
    let id =
      (event.currentTarget.dataset.item &&
        event.currentTarget.dataset.item.id) ||
      0
    wx.navigateTo({
      url: `/pages/${path}/${path}?id=${id}`
    })
  },

  toNewTab(event) {
    let path = event.currentTarget.dataset.path
    wx.switchTab({
      url: `/pages/${path}/${path}`
    })
  },

  search(type) {
    // 查找数据
    this.getBuyShopList(type)
  },

  finish() {
    this.search()
    this.setData({
      show: false
    })
  },

  onCityOptionTap(e) {
    // 选择城市
    const city = e.detail
    this.setData({
      checkedCity: city,
      page: 1
    })
    this.search()
  },

  onResetCity() {
    // 重置城市选项
    this.setData({
      checkedCity: '',
      page: 1
    })
    this.selectComponent('#cityItem').toggle()
    this.selectComponent('#citySelector').reset()
    this.search()
  },

  onResetArea() {
    // 重置面积选项
    this.setData({
      checkedArea: '',
      page: 1
    })
    this.selectComponent('#areaItem').toggle()
    this.selectComponent('#areaSelector').reset()
    this.search()
  },

  onResetWay() {
    // 重置购买方式
    this.setData({
      checkedWay: '',
      page: 1
    })
    this.selectComponent('#wayItem').toggle()
    this.selectComponent('#waySelector').reset()
    this.search()
  },

  onFinish() {
    // 点击确定 收起选项
    this.selectComponent('#cityItem').toggle(false)
    this.selectComponent('#areaItem').toggle(false)
    this.selectComponent('#wayItem').toggle(false)
    this.toPageTop()
  },

  getArea() {
    // 获取商铺面积
    publishModel.getPublicTypes(
      {
        tagKey: '商铺面积',
        type: 1
      },
      (res) => {
        this.setData({
          areaList: res
        })
      }
    )
  },

  getWay() {
    // 获取商铺面积
    publishModel.getPublicTypes(
      {
        tagKey: '购入方式',
        type: 1
      },
      (res) => {
        this.setData({
          wayList: res
        })
      }
    )
  },

  getBuyShopList(type) {
    // 获取招聘列表
    shopModel.buyShopList(
      {
        page: this.data.page,
        location: this.data.checkedCity,
        area: this.data.checkedArea,
        way: this.data.checkedWay,
        title: this.data.searchValue
      },
      (res) => {
        this.setData({
          data: type == 'more' ? [...this.data.data, ...res.rows] : res.rows,
          showLoading: res.rows.length >= 10,
          showDialog: !wx.getStorageSync('showCategoryDialog')
        })
        setTimeout(() => {
          this.setData({
            showSkeleton: false
          })
        }, 500)
        wx.stopPullDownRefresh()
      }
    )
  },

  selectArea(e) {
    // 点击选择店铺面积
    this.setData({
      checkedArea: e.detail.selected,
      page: 1
    })
    this.search()
  },

  selectWay(e) {
    // 点击选择购买方式
    this.setData({
      checkedWay: e.detail.selected,
      page: 1
    })
    this.search()
  },

  init() {
    this.getArea()
    this.getWay()
    this.getBuyShopList()
  },

  touchmove() {
    // 空函数 防止滚动
  },

  onChange(e) {
    this.setData({
      searchValue: e.detail,
      page: 1
    })
  },

  toPageTop() {
    // 切换分类时滚动到顶部
    wx.pageScrollTo({
      scrollTop: 0
    })
  },

  onShowDialog() {
    this.setData({
      showDialog: true
    })
  },

  closeDialog() {
    this.setData({
      showDialog: false
    })
    wx.setStorageSync('showCategoryDialog', true)
  },

  sleep() {
    // 延时 解决部分安卓手机setData过慢，分享图链接不改变问题
    return new Promise((resolve) => setTimeout(resolve, 500))
  },

  changeShareImg() {
    this.setData({
      shareTitle: '技师找工作',
      shareImg: 'https://m.xianmaiyangsheng.com/xcxImg/image/refund-share2.png',
      sharePath: '/pages/shop/candidate'
    })
    setTimeout(() => {
      this.setData({
        shareTitle: '收购足浴店',
        shareImg: '',
        sharePath: '/pages/shop-buy/shop-buy'
      })
    }, 1000)
  },

  /**
   * 用户点击右上角分享
   */
  async onShareAppMessage() {
    this.setData({
      sharePath: `${this.data.sharePath}?otherId=${
        wx.getStorageSync('userInfo').user_id
      }&time=${+new Date()}`
    })
    await this.sleep()
    return {
      title: this.data.shareTitle,
      imageUrl: this.data.shareImg,
      path: this.data.sharePath
    }
  }
})
